package idv.cjcat.pyronova.nodes {
	import flash.geom.Matrix;
	import idv.cjcat.pyronova.geom.Vec2D;
	import idv.cjcat.pyronova.nodes.Group;
	import idv.cjcat.pyronova.render.RenderVisitor;
	import idv.cjcat.pyronova.utils.PyroMath;
	import idv.cjcat.signals.ISignal;
	import idv.cjcat.signals.Signal;
	
	public class Node {
		
		//signals
		//------------------------------------------------------------------------------------------------
		
		private var _onPriorityChange:ISignal = new Signal(Node);
		public function get onPriorityChange():ISignal { return _onPriorityChange; }
		
		//------------------------------------------------------------------------------------------------
		//end of signals
		
		/** @private */
		internal var _parent:Group;
		public function get parent():Group { return _parent; }
		
		/** @private */
		internal var _priority:Number = 0;
		public function get priority():Number { return _priority; }
		public function set priority(value:Number):void {
			if (_priority != value) {
				_priority = value;
				_onPriorityChange.dispatch(this);
			}
		}
		
		//matrix
		//------------------------------------------------------------------------------------------------
		
		private var _matrixDirty:Boolean;
		private var _matrix:Matrix = new Matrix();
		private var _center:Vec2D;
		private var _position:Vec2D;
		private var _rotation:Number;
		private var _scale:Vec2D;
		
		public function get center():Vec2D { return _center; }
		
		public function get position():Vec2D { return _position; }
		
		public function get rotation():Number { return _rotation; }
		public function set rotation(value:Number):void {
			_rotation = value;
			invalidateMatrix();
		}
		
		public function get scale():Vec2D { return _scale; }
		
		private function invalidateMatrix(vec:Vec2D = null):void {
			_matrixDirty = true;
		}
		
		protected function matrix():Matrix {
			if (_matrixDirty) {
				var theta:Number = PyroMath.DEGREE_TO_RADIAN * _rotation;
				var cos:Number = Math.cos(theta);
				var sin:Number = Math.sin(theta);
				var sx:Number = scale.x;
				var sy:Number = scale.y;
				var cx:Number = _center.x;
				var cy:Number = _center.y;
				var px:Number = _position.x;
				var py:Number = _position.y;
				
				_matrix.a = cos * sx;
				_matrix.b = sin * sx;
				_matrix.c = -sin * sy;
				_matrix.d = cos * sy;
				_matrix.tx = -cos * sx * cx + sin * sy * cy + px;
				_matrix.ty = -sin * sx * cx - cos * sy * cy + py;
				
				_matrixDirty = false;
			}
			return _matrix;
		}
		
		//------------------------------------------------------------------------------------------------
		//end of matrix
		
		public function Node() {
			_center = new Vec2D(0, 0);
			_position = new Vec2D(0, 0);
			_rotation = 0;
			_scale = new Vec2D(1, 1);
			
			_center.onChange.add(invalidateMatrix);
			_position.onChange.add(invalidateMatrix);
			_scale.onChange.add(invalidateMatrix);
		}
		
		public function render(visitor:RenderVisitor):void {
			
		}
	}
}